package com.example.app1.web.Test1Validation.validator;

import com.example.app1.web.Test1Validation.annotation.MobilePhone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

public class MobilePhoneValidator implements ConstraintValidator<MobilePhone, String> {

    private static final Pattern PATTERN = Pattern.compile("^1[3-9]\\d{9}$");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null || value.isEmpty()) {
            return true;
        }

        return isMobilePhone(value);
    }

    /**
     * 是手机号码
     */
    private boolean isMobilePhone(CharSequence input) {
        return PATTERN.matcher(input).matches();
    }

}